package org.csu.petserver.web;

import org.csu.petserver.domain.Order;
import org.csu.petserver.domain.OrderData;
import org.csu.petserver.domain.User;
import org.csu.petserver.service.AdminLogin;
import org.csu.petserver.service.Impl.AdminLoginImpl;
import org.csu.petserver.service.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Controller
@Validated
@SessionAttributes(value={"loginUser","orderDatas"})
public class AdminController {
    @Autowired
    AdminLogin adminLogin;
    @Autowired
    OrderInfo orderInfo;
    @Autowired
    User loginUser;

    @RequestMapping("/")
    String loginForm(){
        return "login";
    }

    @RequestMapping("/login")
    String login(@Validated @ModelAttribute User user,
                 BindingResult bindingResult,
                 Model model){
        System.out.println(user.getId()+user.getPassword());
     StringBuilder validationErrorMsg =new StringBuilder();
     if (bindingResult.hasErrors()){
         System.out.println(51846);
         for(ObjectError error : bindingResult.getAllErrors()){
             validationErrorMsg.append(error.getDefaultMessage());
             validationErrorMsg.append(",");
         }
         model.addAttribute("loginMsg",validationErrorMsg.substring(0,validationErrorMsg.length()-1));
         return "login";
     }
     else {
         loginUser=adminLogin.getUserByIdAndPassword(user.getId(), user.getPassword());
         if(loginUser==null){
             //登录失败
             System.out.println(132);
             model.addAttribute("loginMsg","用户名或密码错误");
             return "login";
         }
         else {
             System.out.println(49849);
             model.addAttribute("loginUser",loginUser);
             //登录成功,返回主页面
             List<Order> orders=orderInfo.getAllOrder();
             List<OrderData> orderDatas=new ArrayList<>();

             for(Order order:orders){
                 OrderData orderData=new OrderData();
                 boolean flag=true;
                 for(OrderData orderData1:orderDatas){
                        if(orderData1.getDate().equals(order.getOrderdate())){
                            flag=false;
                            break;
                        }
                 }
                 if(flag){
                 orderData.setDate(order.getOrderdate());
                 try {
                     List<Order> orders1=orderInfo.getOrderByDate(String.valueOf(order.getOrderdate()));
                     orderData.setOrderCount(orders1.size());
                 } catch (ParseException e) {
                     throw new RuntimeException(e);
                 }
                 try {
                     orderData.setOrderAmount(orderInfo.getTodayBanking(String.valueOf(order.getOrderdate())));
                 } catch (ParseException e) {
                     throw new RuntimeException(e);
                 }
                 orderDatas.add(orderData);}
             }
             model.addAttribute("orderDatas",orderDatas );
             return "home";
         }
     }
    }

}
